﻿using AutoMapper;
using Dto;
using Microsoft.AspNetCore.Mvc;
using MyProject;

namespace Controllers
{

    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AddressController : BaseController
    {
      
        public IFreeSql _fsql;
        private readonly IMapper _mapper;

        public AddressController(IFreeSql fsql, IMapper mapper)
        {
            _fsql = fsql;
            _mapper = mapper;
        }

        [HttpPost]
        //[Authorize(Roles ="user")] 
        public ApiModel GetAll([FromBody] ParmId parm)
        {
            var t1 = _fsql.Select<Address>().Where(a => a.Id == parm.Id).ToList();
            if (t1 == null)
            {
                return apiModel(Status.Error, "无数据");
            }
            else
            {
                return apiModel(Status.Success, "", t1);
            }
        }
        /// <summary>
        /// 保存用户地址
        /// </summary>
        public ApiModel Save([FromBody] Address data)
        {
            var t1 = _fsql.InsertOrUpdate<Address>().SetSource(data).ExecuteAffrows();
            if (t1 == 0)
            {
                return apiModel(Status.Error, "保存成功");
            }
            else
            {
                return apiModel(Status.Success, "", t1);
            }
        }
        /// <summary>
        /// 删除地址
        /// </summary>
        public ApiModel Delete([FromBody] ParmId data)
        {
            var t1 = _fsql.Delete<Address>(new { Id = data.Id }).ExecuteAffrows();
            if (t1 == 0)
            {
                return apiModel(Status.Error, "删除错误");
            }
            else
            {
                return apiModel(Status.Success, "", t1);
            }
        }
    }
}
